package main

import "fmt"

func main() {
	map1 := make(map[string]string)     // 无指定容量
	map2 := make(map[string]string, 10) // 指定容量10
	map3 := map[string]string{ //直接初始化
		"username": "admin",
		"password": "admin",
	}
	fmt.Println(map3)

	// 赋值
	map1["username"] = "admin"
	map2["username"] = "admin2"

	// 取值
	username := map1["username"]
	fmt.Println(username)
	fmt.Println(map2["username"])

	// 无效的key
	if _, ok := map1["invalid_key"]; !ok {
		println("key not found")
	}

	// 遍历
	map1["password"] = "admin"
	for key, val := range map1 {
		fmt.Printf("%s --> %s \n", key, val)
	}

	// 值也可以为函数
	myFuncMap := map[string]func() int{
		"myFunc": func() int { return 123 },
	}
	fmt.Println(myFuncMap)
	f := myFuncMap["myFunc"]
	fmt.Println(f())
}
